<?php
	/*	管理员类 */
class Admin extends MySql{
	
	/*检查登陆
	$u_name 邮箱地址
	$u_pwd  用户密码
	返回错误提示或true
	*/
	function checkUser($user,$pwd){
		$data = $this->query($this->select('*','speed_admin','user',$user),'one');
		if(! $data)return false;
		$pwd=md5($pwd);
		if($pwd==$data['user_pwd']){
			$_SESSION['login_admin'] = $user;
			return true;
		}
		return false;
	}
	
	function addAdmin($user,$pwd,$limit){
		$sta = $this->query($this->select('user','speed_admin','user',$user),'num');
		if($sta == 0){
			$data = array("user" => $user,"user_pwd" => md5($pwd),"`limit`" => $limit,"create_time" => date("Y-m-d H:i:s",time()));
			return $this->query($this->insert('speed_admin',$data),'none');
		}else return false;
	}
	
	function checkAdminPwd($pwd){
		$data = $this->query($this->select('user_pwd','speed_admin','user',$_SESSION['login_admin']),'one');
		if(md5($pwd) == $data['user_pwd'])return true;
		else return false;
	}
	
	function updateAdminPwd($pwd){
		return $this->query($this->update('speed_admin',array("user_pwd" => md5($pwd)),'user',$_SESSION['login_admin']),'none');
	}
	
	function delAdmin($id){
		return $this->query($this->del('speed_admin','id',$id),'none');
	}
	
	function getAdmin(){
		$sql = 'select * from speed_admin where `limit`<127';
		return $this->query($sql,'arr');
	}
	
	function checkLogin(){
		if($_SESSION['login_admin'] == ''){
			echo "<script type=\"text/javascript\">location.href='index.php'</script>";
		}else{
			return true;
		}
	}
	
	function loginOut(){
		$_SESSION['login_admin']="";
		}
				
	function checkPwd($pwd){
		if (!ereg("^[_a-zA-Z0-9]*$",$pwd)) {
			return false;
		}
		return true;
	}
	
	function getLimit($user){
		$data = $this->query($this->select("`limit`",'speed_admin','user',$user),'one');
		return $data[0];
	}
	
	function limit($n){
		if($_SESSION['login_admin'] == ''){
			echo "<script type=\"text/javascript\">location.href='index.php'</script>";
		}else{
			$limit = $this->getLimit($_SESSION['login_admin']);
			if($limit&$n){}
			else{
				echo "您的帐号无此权限！";
				echo $limit;
				exit();
			}
		}
	}

}


?>